package com.xzz.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xzz.domain.CourseMarket;
import com.xzz.domain.CourseUserLearn;
import com.xzz.dto.PayResultDto;
import com.xzz.mapper.CourseUserLearnMapper;
import com.xzz.service.ICourseMarketService;
import com.xzz.service.ICourseUserLearnService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xzz
 * @since 2022-09-02
 */
@Service
public class CourseUserLearnServiceImpl extends ServiceImpl<CourseUserLearnMapper, CourseUserLearn> implements ICourseUserLearnService {

    @Autowired
    private ICourseMarketService courseMarketService;

    public CourseUserLearn selectByLoginIdAndCourseId(Long loginId ,Long courseId) {
        Wrapper<CourseUserLearn> wrapper = new EntityWrapper<>();
        wrapper.eq("login_id",loginId);
        wrapper.eq("course_id",courseId);
        return selectOne(wrapper);
    }

    @Override
    public void updateCourseUserLearn(PayResultDto dto) {

        String orderNo = dto.getOrderNo();
        String extParams = dto.getExtParams();

        //数据库中，extParams额外参数，是由map组成的JSONString格式
        Map<String,Object> map = JSON.parseObject(extParams,Map.class);
        Long loginId = Long.valueOf(map.get("loginId").toString());
        String courseIds = map.get("courseIds").toString();
        String[] ids = courseIds.split(",");

        //购物车模式，遍历所有的课程id出来
        for (String courseId : ids) {
            //查询用户的购买记录
            Long courseIdL = Long.valueOf(courseId);
            CourseUserLearn userLearn = selectByLoginIdAndCourseId(loginId, courseIdL);
            //查询课程销售记录
            CourseMarket courseMarket = courseMarketService.selectById(courseId);

            //客户曾经购买，修改用户购买课程的数据，延长时间，覆盖订单号；没有则是新增数据
            Date date = new Date();
            Date endTime = DateUtils.addDays(date, courseMarket.getValidDays());
            if(userLearn != null){
                //追加可观看时间，覆盖订单号
                if(date.before(userLearn.getEndTime())){
                    endTime = DateUtils.addDays(userLearn.getEndTime(), courseMarket.getValidDays());
                }else{
                    userLearn.setStartTime(date);
                }
                userLearn.setEndTime(endTime);
                userLearn.setCourseOrderNo(orderNo);
                updateById(userLearn);
            }else{
                //客户曾经没有购买，则是新增数据
                userLearn = new CourseUserLearn();
                userLearn.setLoginId(loginId);
                userLearn.setStartTime(date);
                userLearn.setEndTime(endTime);
                userLearn.setState(0);
                userLearn.setCourseId(courseIdL);
                userLearn.setCourseOrderNo(orderNo);
                userLearn.setCreateTime(date);
                insert(userLearn);//新增课程购买记录
            }
        }
    }
}
